package cn.message.service.impl;

import cn.itcast.pojo.earnings;
import cn.itcast.pojo.earningsExample;
import cn.itcast.pojo.order;
import cn.message.service.IEarningsService;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 郭智超
 * @create 2020-04-06 14:57
 */
@Service
@Slf4j
public class EarningsServiceImpl implements IEarningsService {

    @Resource
    private cn.itcast.mapper.earningsMapper earningsMapper;



    @Override
    public Map<String, Object> getAllEarnings(Map<String, String> map) {

        System.out.println("开始执行getAllEarnings方法了");
        earningsExample earningsExample = new earningsExample();
        cn.itcast.pojo.earningsExample.Criteria criteria = earningsExample.createCriteria();

        //添加查询条件
        //根据收益来源搜索
        if(!map.get("source").isEmpty()){
            log.debug(map.get("source"));
            criteria.andSourceEqualTo(map.get("source"));
        }
        //根据会员姓名模糊查询
        if(!map.get("name").isEmpty()){
            log.debug(map.get("name"));
            criteria.andNameLike("%"+map.get("name")+"%");
        }
        //根据收益订单号模糊查询
        if(!map.get("number").isEmpty()){
            log.debug(map.get("number"));
            criteria.andNumberLike("%"+map.get("number")+"%");
        }

        //返回符合查询条件的总记录数
        int count = earningsMapper.countByExample(earningsExample);

        int page = Integer.parseInt(map.get("page"));
        int limit = Integer.parseInt(map.get("limit"));
        //分页
        PageHelper.startPage(page, limit);
        //查询所有符合条件的充值记录的信息
        List<earnings> earningsList = earningsMapper.selectByExample(earningsExample);

        Map<String,Object> resultMap = new HashMap<String, Object>();
        resultMap.put("count",count);
        resultMap.put("earningsList",earningsList);

        return resultMap;
    }

    /**
     *功能：给我全部的收益
     * @author 张松
     * @date 2020/6/23
     * @param
     * @return java.util.Map<java.lang.String,java.lang.Object>
     */
    public List<earnings> getAllProfit(String openId){
        List<earnings> allProfit = earningsMapper.getAllProfit(openId);
        earnings sumProfit = earningsMapper.getSumProfit(openId);
        sumProfit.setSource("总收益");
        allProfit.add(sumProfit);
        return allProfit;
    }
}
